//
//  MianPingLunCell.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/21.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "MianPingLunCell.h"

@implementation MianPingLunCell
{
    NSArray *_images;
    NSMutableArray *_imagVs;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)loadCell:(NSDictionary *)data
{
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    _imagVs = [NSMutableArray new];
    //头像
    UIImageView *imageView = [UIImageView new];
    [self.contentView addSubview:imageView];
    [imageView.layer setCornerRadius:18*screenW_6];
    imageView.layer.masksToBounds = YES;
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(17*screenW_6));
        make.height.equalTo(@(36*screenW_6));
        make.width.equalTo(@(36*screenW_6));
        make.top.equalTo(self.contentView).offset(18*screenW_6);
    }];
    imageView.clipsToBounds = YES;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    //姓名
    UILabel *nameLabel = [UILabel new];
    [self.contentView addSubview:nameLabel];
    nameLabel.textColor = TextBlack;
    nameLabel.font = [UIFont systemFontOfSize:14*screenW_6];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(15*screenW_6);
        make.top.equalTo(self.contentView).offset(18*screenW_6);
        make.height.equalTo(@(18*screenW_6));
        make.width.equalTo(self.contentView).offset(-15*screenW_6);
    }];
    
    //时间
    UILabel *timeLabel = [UILabel new];
    timeLabel.textColor = TextGray;
    timeLabel.font = [UIFont systemFontOfSize:12*screenW_6];
    [self.contentView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(15*screenW_6);
        make.top.equalTo(nameLabel.mas_bottom).offset(0);
        make.height.equalTo(@(18*screenW_6));
        make.width.equalTo(@(SCREENW-80*screenW_6));
    }];
    
    //内容
    UILabel *contentLabel = [UILabel new];
    [self.contentView addSubview:contentLabel];
    contentLabel.numberOfLines = 0;
    contentLabel.textColor = TextBlack;
    contentLabel.font = [UIFont systemFontOfSize:15*screenW_6];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(15*screenW_6);
        make.top.equalTo(timeLabel.mas_bottom).offset(5*screenW_6);
        make.width.equalTo(@(SCREENW-80*screenW_6));
    }];

    //图片
    _images = data[@"imgList"];
    if (_images.count) {
        UIScrollView *imageSrollV = [UIScrollView new];
        imageSrollV.contentSize = CGSizeMake(60*screenW_6 *_images.count -10*screenW_6, 50*screenW_6);
        imageSrollV.scrollEnabled = YES;
        imageSrollV.showsHorizontalScrollIndicator = NO;
        imageSrollV.showsVerticalScrollIndicator = NO;
        [self.contentView addSubview:imageSrollV];
        for (int i = 0; i<_images.count; i++) {
            UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(60*i, 0, 50*screenW_6, 50*screenW_6)];
            [imageV sd_setImageWithURL:_images[i][@"image"] placeholderImage:nil];
            [imageSrollV addSubview:imageV];
            imageV.clipsToBounds = YES;
            imageV.contentMode = UIViewContentModeScaleAspectFill;
            [_imagVs addObject:imageV];
            imageV.userInteractionEnabled = YES;
            [imageV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)]];
        }
        [imageSrollV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imageView.mas_right).offset(15*screenW_6);
            make.top.equalTo(contentLabel.mas_bottom).offset(10*screenW_6);
            make.width.equalTo(@(SCREENW-70*screenW_6));
            make.height.equalTo(@(50*screenW_6));
        }];
    }
    
    //线条
    UIView *lineView = [UIView new];
    lineView.backgroundColor = LineColor;
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15*screenW_6);
//        make.top.equalTo(imageSrollV.mas_bottom).offset(20*screenW_6);
        make.width.equalTo(@(SCREENW-30*screenW_6));
        make.height.equalTo(@(1*screenW_6));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
    }];
    
    //数据加载
    //    [imageView sd_setImageWithURL:[NSURL URLWithString:data[@"user"]?data[@"user"][@"photo"]:@""] placeholderImage:[UIImage imageNamed:@"backImage"]];
    [imageView sd_setImageWithURL:[NSURL URLWithString:data[@"headPortrait"]] placeholderImage:[UIImage imageNamed:@"forumHeader"]];
    nameLabel.text = data[@"nickName"];
    timeLabel.text = @"2017.11.14 12:44";
    contentLabel.text = data[@"comment"];
}

-(void)showImage:(UITapGestureRecognizer *)tap
{
    NSMutableArray *items = [NSMutableArray array];
    UIView *fromView = tap.view;
    for (int i =0; i < _images.count; i++) {
        YYPhotoGroupItem *item = [YYPhotoGroupItem new];
        item.thumbView = _imagVs[i];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",_images[i][@"image"]]];
        item.largeImageURL = url;
        [items addObject:item];
    }
    YYPhotoBrowseView *groupView = [[YYPhotoBrowseView alloc]initWithGroupItems:items];
    [groupView presentFromImageView:fromView toContainer:[UIApplication sharedApplication].keyWindow
                           animated:YES completion:nil];
}

@end
